package com.ymatou.iapi.sellercs.testcase;

import java.math.BigDecimal;
import java.util.List;

import org.joda.time.DateTime;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.sellercs.parameter.CheckRiskyUserBean;
import com.ymatou.iapi.sellercs.parameter.SendMessageBean;
import com.ymatou.iapi.sellercs.service.CheckRiskyUserCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.model.YmtRiskyuser;
import com.ymttest.database.sharding.db.sharding.model.AutoReceiveOptPo;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*检查风险买家
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CheckRiskyUser{
	private static CheckRiskyUserBean checkriskyuserBean;
	private static CheckRiskyUserCall checkriskyuserCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("检查风险买家");
	}

	@Before
	public void caseUp() {
		checkriskyuserBean=new CheckRiskyUserBean();
		checkriskyuserCall=new CheckRiskyUserCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(checkriskyuserCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckRiskyUser_001() {
		Logger.start(true,"检查风险买家成功,进入风险表");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			String riskmsg="http://www.baidu.com/G01/M00/3A/B0/rBBlD1n0JquAMmYrAAHCOb9q0j4447_3_4_c.jpg#184x246"+TestDataManager.getRandomNum(5);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, riskmsg);
			SellerCSIMCallService.SendMessage(sendmessageBean);
			sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, 3383, riskmsg);
			SellerCSIMCallService.SendMessage(sendmessageBean);
			sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, 288484, riskmsg);
			SellerCSIMCallService.SendMessage(sendmessageBean);
			sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, 20123134, riskmsg);
			SellerCSIMCallService.SendMessage(sendmessageBean);
			Thread.sleep(3000);
			checkriskyuserBean.setAppId("auto");
			checkriskyuserCall.setData(checkriskyuserBean);
			checkriskyuserCall.callService();			
			Thread.sleep(1000);
			Logger.verifyEquals("ok", checkriskyuserCall.getReturnData(), "验证返回");
			List<YmtRiskyuser> list = YmtSellerCSIMWapper.selectYmtRiskyuserByUid(userId);
			int redo=0;
			while((list==null||list.isEmpty())&&redo<3){
				Thread.sleep(1000);
				 list = YmtSellerCSIMWapper.selectYmtRiskyuserByUid(userId);
				 redo++;
			}
			Logger.verifyEquals(1, list.size(), "验证进入风控");
			Logger.verifyEquals(true, list.get(0).getDisabled(), "验证生效");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckRiskyUser_002() {
		Logger.start(true,"60分钟内给1个买手发送风险消息不进风险表");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			String riskmsg="http://www.baidu.com/G01/M00/3A/B0/rBBlD1n0JquAMmYrAAHCOb9q0j4447_3_4_c.jpg#184x246"+TestDataManager.getRandomNum(5);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, riskmsg);
			SellerCSIMCallService.SendMessage(sendmessageBean);
			
			checkriskyuserBean.setAppId("auto");
			checkriskyuserCall.setData(checkriskyuserBean);
			checkriskyuserCall.callService();			
			Thread.sleep(1000);
			Logger.verifyEquals("ok", checkriskyuserCall.getReturnData(), "验证返回");
			List<YmtRiskyuser> list = YmtSellerCSIMWapper.selectYmtRiskyuserByUid(userId);
			Logger.verifyEquals(0, list.size(), "验证没有进入风控");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckRiskyUser_003() {
		Logger.start(true,"有订单的用户不进风险表");
		try {
			Logger.comment("注册新用户");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long sellerId2 = placeOrderResp.getOrderList().get(0).getSellerId();

			Integer userId = ((Long)placeOrderResp.getUserId()).intValue();

			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();
			//订单支付
			TradingCallService.payOrder(userId, useAccountAmount, tradingId);
			//接单
			TradingCallService.sellerAcceptOrder(sellerId2, orderId);
			//发货
			TradingCallService.delivery(sellerId2, orderId, false);

			//自动确认收货时间操作表（auto_receive_opt） 
			AutoReceiveOptPo record = new AutoReceiveOptPo();
			record.setOrderId(orderId);
			record.setExpectReceiveTime(DateTime.now().plusDays(-1).toDate());


			Logger.comment("前置数据准备结束");
			
			
			String riskmsg="http://www.baidu.com/G01/M00/3A/B0/rBBlD1n0JquAMmYrAAHCOb9q0j4447_3_4_c.jpg#184x246"+TestDataManager.getRandomNum(5);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, riskmsg);
			SellerCSIMCallService.SendMessage(sendmessageBean);
			sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, 3383, riskmsg);
			SellerCSIMCallService.SendMessage(sendmessageBean);
			sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, 288484, riskmsg);
			
			SellerCSIMCallService.SendMessage(sendmessageBean);
			checkriskyuserBean.setAppId("auto");
			checkriskyuserCall.setData(checkriskyuserBean);
			checkriskyuserCall.callService();			
			Logger.verifyEquals("ok", checkriskyuserCall.getReturnData(), "验证返回");
			Thread.sleep(1000);
			List<YmtRiskyuser> list = YmtSellerCSIMWapper.selectYmtRiskyuserByUid(userId);
			Logger.verifyEquals(0, list.size(), "验证进入风控");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckRiskyUser_004() {
		Logger.start(true,"7天前的数据不处理");
		try {
			//使用存量数据
			checkriskyuserCall.setData(checkriskyuserBean);
			checkriskyuserCall.callService();			
			checkriskyuserBean.setAppId("auto");
			checkriskyuserCall.setData(checkriskyuserBean);
			checkriskyuserCall.callService();			
			Logger.verifyEquals("ok", checkriskyuserCall.getReturnData(), "验证返回");
			Thread.sleep(1000);
			List<YmtRiskyuser> list = YmtSellerCSIMWapper.selectYmtRiskyuserByUid(24720805);
			Logger.verifyEquals(0, list.size(), "验证进入风控");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}